import { makeAutoObservable } from 'mobx';
import { Vector, VectorFormValues } from './vectorTypes';
import { ModelOption} from '../../agent/store/agentTypes';
import { getVectorList, createVector, updateVector, getInfo, deleteVector, getActiveModel } from '../../../../apis/backhub/vector';
import { message } from 'antd';

class VectorStore {
  vectors: Vector[] = [];
  modalVisible = false; // 弹窗显示状态
  editingVector: Vector | undefined = undefined; // 当前编辑的向量知识库，undefined表示新建
  loading = false;
  modelValues: any;
  modelOptions: ModelOption[] = [];

  constructor() {
    makeAutoObservable(this);
  }

  // 获取向量知识库列表
  fetchVectors = async () => {
    this.loading = true;
    try {
      const response = await getVectorList();
      this.vectors = (response as any)?.data;
      return this.vectors; 
    } catch (error) {
      console.error('获取向量知识库列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 获取所有选项数据
  fetchModelOptions = async () => {
    try {
      const response = await getActiveModel();
      this.modelValues=[];
      this.modelOptions=[];
      this.modelValues = (response as any)?.data;
      this.modelValues && Object.entries(this.modelValues).map(([roleId, config]) => (
        this.modelOptions.push(config as any)
      ));
    } catch (error) {
      console.error('获取智能体列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 显示创建弹窗
  showCreateModal = () => {
    this.editingVector = undefined;
    this.modalVisible = true;
  };

  // 显示编辑弹窗
  showEditModal = (vector: Vector) => {
    this.modalVisible = true;
  };

  // 隐藏弹窗
  hideModal = () => {
    this.modalVisible = false;
    this.editingVector = undefined;
  };

  //获取模型
  getVector = async (id: string) => {
    try {
      this.loading = true;
      const response = await getInfo(id);
      this.editingVector = (response as any).data;
      return this.editingVector; 
    } catch (error) {
      console.error('获取用户失败:', error);
      throw error;
    } finally {
      this.loading = false;
    }
  };

  // 保存向量知识库
  saveVector = async (values: VectorFormValues) => {
    try {
      if (this.editingVector && this.editingVector.id) {
        const response = await updateVector(this.editingVector.id, values);
        if((response as any)?.status){
          message.success('保存成功');
          this.fetchVectors();
        }
      } else {
        const response = await createVector(values);
        if((response as any)?.status){
          message.success('创建成功');
          this.fetchVectors();
        }
      }
      this.hideModal();
    } catch (error) {
      console.error('保存向量知识库失败:', error);
    }
  };

  // 删除向量知识库
  deleteVector = async (id: string) => {
    try {
      const response = await deleteVector(id);
      if((response as any)?.status){
        message.success('删除成功');
        this.fetchVectors();
      }
    } catch (error) {
      console.error('删除向量知识库失败:', error);
    }
  };
}

export const vectorStore = new VectorStore();